<template>
  <div class="publish-view" ref="publishEditRef">
    <div class="side-menu">
      <div class="side-wrapper">
        <div class="appmsg">
          <div class="appmsg-preview">
            <div class="appmsgItem" v-for="(msg,index) in articlesAdd" :key="index" :class="{'current':isActiveAddNews==index,'first_card_media':index==0,'sub_card_media':index!=0}" @click="isActiveAddNews=index">
              <el-popover
                  placement="right"
                  :width="40"
                  trigger="click"
                  :show-arrow="false"
                  :teleported="false"
                  popper-class="changemsg-popover"
                  :disabled="articlesAdd.length==1"
                  :offset="5"
              >
                <template #reference>
                  <div class="appmsg-item">
                    <div class="card-container">
                      <div class="bottom-title">
                        <p class="tit">{{msg.title?msg.title:'标题'}}</p>
                      </div>
                      <!-- 封面图 -->
                      <!-- <div class="img" :style="{ backgroundImage: `url(${msg.thumb_url})` }"> -->
                      <div class="img">
                        <el-image :src="msg.thumb_url" style="width: 100%; height: 100%;" fit="contain" v-if="msg.thumb_url"/>
                      </div>
                    </div>
                  </div>
                </template>
                <div class="tool-wrap">
                  <div class="down tool-btn" v-if="isActiveAddNews!=articlesAdd.length-1" @click="clickDown(index)"><el-icon size="20"><Bottom /></el-icon></div>
                  <el-popconfirm
                      width="220"
                      confirm-button-text="确认"
                      cancel-button-text="取消"
                      title="确认删除此消息吗?删除后将无法恢复，需重新生成！"
                      @confirm="deleteMsg(index)"
                      v-if="isActiveAddNews!=0"
                      :hide-icon="true"
                      placement="right"
                  >
                    <template #reference>
                      <div class="delete tool-btn" v-if="articlesAdd.length>0"> <el-icon size="20"><Delete /></el-icon></div>
                    </template>
                  </el-popconfirm>
                  <div class="down tool-btn" v-if="isActiveAddNews!=0" @click="clickUp(index)"><el-icon size="20"><Top /></el-icon></div>
                </div>
              </el-popover>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="right-form">
      <el-scrollbar>
        <div class="inner-form" v-for="(item,index) in articlesAdd" :key="index" v-show="isActiveAddNews == index">
          <el-form :model="item" ref="formRefs" :rules="addMsgRules">
            <el-form-item prop="title">
              <el-input size="large" v-model="item.title" placeholder="请在这里输入标题"></el-input>
            </el-form-item>
            <el-form-item prop="author">
              <el-input size="large" v-model="item.author" placeholder="请在这里输入作者"></el-input>
            </el-form-item>
            <div class="cover-digest">
              <h2>封面和摘要</h2>
              <div class="inner-cover-digest">
                <WechatThumb
                    :content="item.content"
                    ruleName="封面"
                    v-model:value="item.thumb_url"
                    :grabField="{
                                            field: 'content',
                                            name: '正文',
                                        }"
                    field="thumb_url"
                    :required="true"/>
                <el-form-item style="margin-left:20px;flex:1">
                  <el-input v-model="item.digest" :rows="5" type="textarea" placeholder="摘要"/>
                </el-form-item>
              </div>
            </div>
            <el-form-item prop="content">
              <TinymceEditor v-model:modelValue="item.content" :minHeight="500" :editorName="index.toString()"/>
            </el-form-item>
          </el-form>
        </div>
      </el-scrollbar>
    </div>
  </div>
</template>

<script setup>
  import { Bottom, Delete, Top } from '@element-plus/icons-vue'
  import { ref, defineProps, watch, defineExpose } from "vue";
  const props = defineProps({
    articlesAdd: {
      type: Array,
      default: null
    }
  })
  const isActiveAddNews = ref(0)
  const articlesAdd = ref(props.articlesAdd ? props.articlesAdd : [{title:'',
    content:'',
    thumb_url:'',
    author:'',
    digest:''}])
  const addMsgRules = {
    title:[
      { required: true, message: '请输入标题', trigger: 'blur' },
      { max: 64, message: '长度限制64个字符', trigger: 'blur' },
    ],
    content:[
      { required: true, message: '请输入正文', trigger: 'blur' }
    ],
    author: [
      { required: true, message: '请输入作者', trigger: 'blur'}
    ]
  }
  watch(() => props.articlesAdd, (newVal) => {
    isActiveAddNews.value = 0
    articlesAdd.value = newVal ? newVal : [{title:'', content:'', thumb_url:'', author:'', digest:''}]
  },{deep: true})
  //下移
  const clickDown = (index) => {
    // console.log(index)
    articlesAdd.value.splice(index, 1, ...articlesAdd.value.splice(index + 1, 1, articlesAdd.value[index]));
    isActiveAddNews.value = index + 1
  }
  //上移动
  const clickUp = (index) => {
    articlesAdd.value.splice(index - 1, 1, ...articlesAdd.value.splice(index, 1, articlesAdd.value[index - 1]));
    isActiveAddNews.value = index - 1
  }
  //删除一个图文
  const deleteMsg = (index) => {
    articlesAdd.value.splice(index, 1);
    isActiveAddNews.value--
    if(isActiveAddNews.value<0) {
      isActiveAddNews.value = 0
    }
  }
  const formRefs = ref()
  const getArticles = async () => {
    let flag = true
    // 表单验证
    if(!formRefs.value) return null
    for (let i = 0; i < formRefs.value.length; i++) {
      await formRefs.value[i].validate((valid) => {
        if (!valid) {
          console.log(1,i)
          isActiveAddNews.value = i
          flag = false;
        }

      })
      if(flag == false) {
        break;
      }
    }
    if(flag) {
      return articlesAdd.value
    }else{
      return false
    }
  }
  defineExpose({
    getArticles
  })

</script>

<style scoped lang="scss">
.publish-view {
  display: flex;
  justify-content: space-between;
  .side-menu {
    width: 35%;
    .side-wrapper {
      box-shadow: 0 0 8px 0 rgba(229, 229, 229, 0.5);
      margin: 0;
      position: relative;
      width: 310px;
      margin: auto;
      .appmsg {
        background-color: #ffffff;
        color: #666666;
        overflow: visible;
        margin-bottom: 0;
        border: none;
        position: relative;
        .appmsg-preview {
          .appmsgItem {
            cursor: pointer;
            &.first_card_media {
              .appmsg-item {
                .card-container {
                  height: 176px;
                  position: relative;
                  .img {
                    position: absolute;
                    top: 0;
                    bottom: 0;
                    left: 0;
                    right: 0;
                    background-repeat: no-repeat;
                    background-size: contain;
                    background-position: center;
                    &:before {
                      content: "";
                      content: " ";
                      position: absolute;
                      left: 0;
                      right: 0;
                      top: 0;
                      bottom: 0;
                      background-color: rgba(0, 0, 0, 0.3);
                      z-index: 1;
                    }
                  }
                  .bottom-title {
                    position: absolute;
                    bottom: 0;
                    left: 0;
                    box-sizing: border-box;
                    padding: 15px;
                    display: flex;
                    justify-content: space-between;
                    z-index: 2;
                    width: 100%;
                    .tit {
                      @include textEllipsis(23px,2);
                      color:#fff;
                    }
                  }
                }
              }
            }
            &.sub_card_media {
              position: relative;
              &:before {
                content: "";
                width:calc(100% - 20px);
                position:absolute;
                bottom:0;
                left:10px;
                border-bottom:1px solid #eaeaea;
              }
              &:last-of-type:before {
                display: none;
              }
              .appmsg-item {
                position: relative;
                z-index: 1;
                .card-container {
                  padding: 12px;
                  height: 72px;
                  box-sizing:border-box;
                  display: flex;
                  justify-content: space-between;
                  align-items:center;
                  .img {
                    width: 48px;
                    height: 48px;
                    position: relative;
                    flex-shrink: 0;
                    &:before {
                      content: "";
                      content: " ";
                      position: absolute;
                      left: 0;
                      right: 0;
                      top: 0;
                      bottom: 0;
                      background-color: rgba(0, 0, 0, 0.3);
                    }
                  }
                  .bottom-title {
                    margin-right: 10px;
                    .tit {
                      @include textEllipsis(23px,2);
                    }
                  }
                }
              }
            }
            &.current {
              .appmsg-item {
                border: 2px solid var(--el-color-primary);
              }
            }
          }
        }
      }
      .add-appmsg-container {
        height: 90px;
        position: relative;
        margin-bottom: 0;
        border-radius: 0 0 5px 5px;
        background: #fff;
        border-top: 1px solid #EBEBEB;
        .add-btn {
          color: #9A9A9A;
          cursor: pointer;
          .add-word {
            display: inline-block;
            margin-left: 5px;
          }
        }
      }
    }
  }
  .right-form {
    width: 60%;
    height: 60vh;
    overflow: auto;
    .inner-form {
      .cover-digest {
        h2 {
          font-weight: normal;
          margin-bottom: 20px;
        }
        .inner-cover-digest {
          display: flex;
          align-items: flex-start;
        }
      }
    }
  }
}
::v-deep(.el-popover.changemsg-popover) {
  min-width: 30px;
  padding: 2px!important;
  border-radius: 99px!important;
  .tool-wrap {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content:space-between;
    .tool-btn {
      cursor: pointer;
      width: 30px;
      height: 30px;
      border-radius:50%;
      text-align:center;
      line-height: 35px;
      &:hover {
        background-color: rgba(#ccc,0.5);
      }
    }
  }
}
</style>
